package by.grsu.coctails;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import by.grsu.coctails.adapter.MainPagerAdapter;

import com.jfeinstein.jazzyviewpager.JazzyViewPager;
import com.jfeinstein.jazzyviewpager.JazzyViewPager.TransitionEffect;

public class MainActivity extends FragmentActivity implements OnPageChangeListener {

	private static final String TAG = MainActivity.class.getSimpleName();

	private JazzyViewPager mViewPager;

	private ImageView myCoctailSelectorView;

	private ImageView myIngredientsSelectorView;

	private ImageView coctailSelectorView;

	private ImageView ingredientsSelectorView;

	private ImageView[] bottomButtons;

	private int lastPosition = 2;

	private RelativeLayout pagerLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		pagerLayout = (RelativeLayout) findViewById(R.id.pagerView);
		myCoctailSelectorView = (ImageView) findViewById(R.id.myCoctailSelector);
		myIngredientsSelectorView = (ImageView) findViewById(R.id.myIngredientsSelector);
		coctailSelectorView = (ImageView) findViewById(R.id.coctailsSelector);
		ingredientsSelectorView = (ImageView) findViewById(R.id.ingredientsSelector);
		bottomButtons = new ImageView[] { myCoctailSelectorView, myIngredientsSelectorView, coctailSelectorView,
				ingredientsSelectorView };

		mViewPager = (JazzyViewPager) findViewById(R.id.jazzyPager);
		mViewPager.setTransitionEffect(TransitionEffect.Tablet);
		mViewPager.setOffscreenPageLimit(4);

		MainPagerAdapter adapter = new MainPagerAdapter(mViewPager, getSupportFragmentManager(), this);
		mViewPager.setAdapter(adapter);

		mViewPager.setCurrentItem(lastPosition);
		mViewPager.setOnPageChangeListener(this);
		onPageSelected(lastPosition);

	}

	public void onMyCoctailsClick(View view) {
		onBottomButtonClick(0);
	}

	public void onMyIngredientsClick(View view) {
		onBottomButtonClick(1);
	}

	public void onCoctailsClick(View view) {
		onBottomButtonClick(2);
	}

	public void onIngredientsClick(View view) {
		onBottomButtonClick(3);
	}

	private void onBottomButtonClick(int position) {
		mViewPager.setCurrentItem(position);
		hidePagerView();
		removeBackStackFragments();
	}

	private void removeBackStackFragments() {
		for (int i = 0; i < getSupportFragmentManager().getBackStackEntryCount(); i++) {
			getSupportFragmentManager().popBackStack();
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	private void hidePagerView() {
		if (pagerLayout.getVisibility() == View.VISIBLE) {
			pagerLayout.setVisibility(View.GONE);
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
			hidePagerView();
		}
	}

	@Override
	public void onPageSelected(int newPosition) {
		bottomButtons[lastPosition].setVisibility(View.INVISIBLE);
		bottomButtons[newPosition].setVisibility(View.VISIBLE);
		lastPosition = newPosition;
	}
}
